﻿using System;
using Infrastructure.Assets.Resources;
using Infrastructure.Interfaces;
using Infrastructure.Tools.Events;

namespace Infrastructure.Controls
{
    public partial class ErrorWindow : IErrorWindow
    {
        public ErrorWindow()
        {
            InitializeComponent();
            Title = ErrorResources.ErrorLabel;
        }

        private Exception _exception;
        public Exception Exception 
        { 
            get { return _exception; }
            set 
            { 
                _exception = value;
                SetMessage();
                
            }
        }

        private string _message;
        public string Message
        {
            get { return _message; }
            set 
            { 
                _message = value;
                SetMessage();
            }
        }

        private void SetMessage()
        {
            if (_exception == null)
            {
                lblError.Text = _message;
            }
            else
            {
                var type = _exception.GetType().Name;
                switch (type)
                {
                    case "EntityDomainOperationException":
                        var edoex = _exception as EntityDomainOperationException;
                        if (edoex != null)
                        {
                            var errCode = edoex.ErrorCode;
                            switch (errCode)
                            {
                                case 401:
                                    lblError.Text = ErrorResources.UnAuthorizedOperation;
                                    break;
                                default:
                                    lblError.Text = ErrorResources.UnidentifiedError;
                                    break;
                            }
                        }
                        break;
                    default:
                        lblError.Text = _exception.Message;
                        break;
                }
            }
        }

        private void btnOk_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.Close();
        }
    }
}

